﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace Autorun
{

	[Serializable()]
	public class ScheduleItem : MarshalByRefObject
	{
		private int schId;
		private int fktskId;
		private ScheduleTypes schType;
		private DateTime schStartDateTime;
		private DateTime schFinishDate;
		private DateTime schFinishTime;
		private TimeSpan schRepeatEvery;
		private ScheduleWeekDays schRunOnWeekDays;
		private ScheduleMonths schRunOnMonths;
		private int schRunOnDays;
		private ScheduleRunOnDayOccurences schRunOnDayOccurence;

		private string tskCaption;

		#region "Constructors"
		public ScheduleItem(DataRow row)
		{
			schId = (int)row["schId"];
			fktskId = (int)row["fktskId"];
			schType = (ScheduleTypes)((byte)row["schType"]);
			schStartDateTime = (DateTime)row["schStartDateTime"];
			schFinishDate = (DBNull.Value.Equals(row["schFinishDate"]) == true ? DateTime.MaxValue : (DateTime)row["schFinishDate"]);
			schFinishTime = (DBNull.Value.Equals(row["schFinishTime"]) == true ? DateTime.MaxValue : (DateTime)row["schFinishTime"]); ;
			schRepeatEvery = (DBNull.Value.Equals(row["schRepeatEvery"]) == true ? TimeSpan.Zero : new TimeSpan((long)row["schRepeatEvery"]));
			schRunOnWeekDays = (ScheduleWeekDays)((byte)row["schRunOnWeekDays"]);
			schRunOnMonths = (ScheduleMonths)((Int16)row["schRunOnMonths"]);
			schRunOnDays = (int)row["schRunOnDays"];
			schRunOnDayOccurence = (ScheduleRunOnDayOccurences)((byte)row["schRunOnDayOccurence"]);

			tskCaption = row["tskCaption"].ToString();
		}

		internal ScheduleItem(int scheduleId)
		{
			string query;
			DataTable dt;

			query = string.Format("SELECT * FROM tblSchedule WHERE schId={0}", scheduleId);
			dt = ExecuteDataTable(query);

			if (dt.Rows.Count != 0)
			{
				schId = (int)dt.Rows[0]["schId"];
				fktskId = (int)dt.Rows[0]["fktskId"];
				schType = (ScheduleTypes)((byte)dt.Rows[0]["schType"]);
				schStartDateTime = (DateTime)dt.Rows[0]["schStartDateTime"];
				schFinishDate = (DBNull.Value.Equals(dt.Rows[0]["schFinishDate"]) == true ? DateTime.MaxValue : (DateTime)dt.Rows[0]["schFinishDate"]);
				schFinishTime = (DBNull.Value.Equals(dt.Rows[0]["schFinishTime"]) == true ? DateTime.MaxValue : (DateTime)dt.Rows[0]["schFinishTime"]);
				schRepeatEvery = (DBNull.Value.Equals(dt.Rows[0]["schRepeatEvery"]) == true ? TimeSpan.Zero : new TimeSpan((long)dt.Rows[0]["schRepeatEvery"]));
				schRunOnWeekDays = (ScheduleWeekDays)((byte)dt.Rows[0]["schRunOnWeekDays"]);
				schRunOnMonths = (ScheduleMonths)((Int16)dt.Rows[0]["schRunOnMonths"]);
				schRunOnDays = (int)dt.Rows[0]["schRunOnDays"];
				schRunOnDayOccurence = (ScheduleRunOnDayOccurences)((byte)dt.Rows[0]["schRunOnDayOccurence"]);
			}
		}
		#endregion

		#region "Properties"
		public string Caption
		{
			get { return tskCaption; }
		}

		public int Id
		{
			get { return schId; }
		}

		public int TaskId
		{
			get { return fktskId; }
			set { fktskId = value; }
		}

		public ScheduleTypes Types
		{
			get { return schType; }
			set { schType = value; }
		}

		public DateTime StartDateTime
		{
			get { return schStartDateTime; }
			set { schStartDateTime = value; }
		}

		public DateTime FinishDate
		{
			get { return schFinishDate; }
			set { schFinishDate = new DateTime(value.Year, value.Month, value.Day, 23, 59, 59); }
		}

		public DateTime FinishTime
		{
			get { return schFinishTime; }
			set { schFinishTime = value; }
		}

		public TimeSpan RepeatEvery
		{
			get { return schRepeatEvery; }
			set { schRepeatEvery = value; }
		}

		public ScheduleWeekDays RunOnWeekDays
		{
			get { return schRunOnWeekDays; }
			set { schRunOnWeekDays = value; }
		}

		public ScheduleMonths RunOnMonths
		{
			get { return schRunOnMonths; }
			set { schRunOnMonths = value; }
		}

		public int RunOnDays
		{
			get { return schRunOnDays; }
			set { schRunOnDays = value; }
		}

		public ScheduleRunOnDayOccurences RunOnDayOccurences
		{
			get { return schRunOnDayOccurence; }
			set { schRunOnDayOccurence = value; }
		}
		#endregion

		#region "Procedures"
		public void Update()
		{
			string query;

			query = string.Format("UPDATE tblSchedule SET fktskId={0}, schType={1}, schStartDateTime={2}, schFinishDate={3}, schFinishTime={4}, schRepeatEvery={5}, schRunOnWeekDays={6}, schRunOnMonths={7}, schRunOnDays={8}, schRunOnDayOccurence={9} WHERE schId={10}",
				fktskId,
				(int)schType,
				SqlConvert.SqlDate(schStartDateTime),
				SqlConvert.SqlDate(schFinishDate),
				SqlConvert.SqlDate(schFinishTime),
				schRepeatEvery.Ticks,
				(int)schRunOnWeekDays,
				(int)schRunOnMonths,
				(int)schRunOnDays,
				(int)schRunOnDayOccurence,
				schId);
			ExecuteNonQuery(query);
		}
		#endregion

		#region "Data Access Methods"
		private void ExecuteNonQuery(string query)
		{
			SqlConnection cn;
			SqlCommand cmd;

			cn = new SqlConnection(Properties.Settings.Default.ConnectionString);

			try
			{
				cn.Open();
				cmd = cn.CreateCommand();
				cmd.CommandText = query;
				cmd.ExecuteNonQuery();
				cmd.Connection = null;
				cmd = null;
			}
			finally
			{
				cn.Close();
			}
		}

		private object ExecuteScalar(string query)
		{
			SqlConnection cn;
			SqlCommand cmd;
			object rc = null;

			cn = new SqlConnection(Properties.Settings.Default.ConnectionString);

			try
			{
				cn.Open();
				cmd = cn.CreateCommand();
				cmd.CommandText = query;
				rc = cmd.ExecuteScalar();
				cmd.Connection = null;
				cmd = null;
			}
			finally
			{
				cn.Close();
			}

			return rc;
		}

		private DataTable ExecuteDataTable(string query)
		{
			SqlDataAdapter da;
			DataSet rc;

			da = new SqlDataAdapter(query, Properties.Settings.Default.ConnectionString);
			rc = new DataSet();
			da.Fill(rc);
			return rc.Tables.Count != 0 ? rc.Tables[0] : null;
		}

		private DataSet ExecuteDataSet(string query)
		{
			SqlDataAdapter da;
			DataSet rc;

			da = new SqlDataAdapter(query, Properties.Settings.Default.ConnectionString);
			rc = new DataSet();
			da.Fill(rc);
			return rc;
		}
		#endregion
	}
}
